package com.itheima.health.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.health.dto.CheckGroupDTO;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.mapper.CheckGroupMapper;
import com.itheima.health.mapper.CheckgroupCheckitemMapper;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.service.CheckGroupService;
import com.itheima.health.service.CheckItemService;
import com.itheima.health.vo.CheckGroupVO;
import com.sun.corba.se.spi.orbutil.threadpool.WorkQueue;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;


@Service
@Transactional
public class CheckGroupServiceImpl extends ServiceImpl<CheckGroupMapper, CheckGroup> implements CheckGroupService {

    @Autowired
    CheckgroupCheckitemMapper checkgroupCheckitemMapper;

    @Autowired
    private CheckItemService checkItemService;

    @Override
    public Boolean saveCheckGroup(CheckGroupDTO checkGroupDTO) {
        boolean flag = this.save(checkGroupDTO);
        Integer id = checkGroupDTO.getId();
        Integer[] checkitemIds = checkGroupDTO.getCheckitemIds();

        for (Integer checkitemId : checkitemIds) {
            checkgroupCheckitemMapper.save(id, checkitemId);
        }
        return flag ;
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {

        QueryWrapper<CheckGroup> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("IS_DELETE",0);

        Page<CheckGroup> page;
        if(StringUtils.isNotBlank(queryPageBean.getQueryString())){
            queryWrapper.like("NAME",queryPageBean.getQueryString());
            page = this.page(new Page<CheckGroup>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize()),queryWrapper);

        }else {
            page = this.page(new Page<CheckGroup>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize()),queryWrapper);

        }
        return new PageResult(page.getTotal(),page.getRecords());
    }

    @Override
    public CheckGroupVO findCheckItemInfoByGroupId(int id) {

        //   查询所有检查项信息  +  gid  查询对应得 checkitemids 中间表
        List<CheckItem> list = checkItemService.list();
        int[] checkitemids = baseMapper.findCheckItemInfoByGroupId(id); //  一表
        CheckGroupVO checkGroupVO = new CheckGroupVO();
        checkGroupVO.setCheckItemList(list); //  将查询的结果集封装到vo 中
        checkGroupVO.setCheckitemIds(checkitemids);
        return checkGroupVO;
    }

    @Override
    public List<CheckItem> findCheckItemByGid(Integer id) {
        return null;
    }
}
